package com.innovative.androiddemo1;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import android.content.Context;
import android.util.Log;

public class WebServices {
	Context context;
	C c;

	String host = "http://demo.foodme.ro/FoodMe/MobileClientService.svc/json/";
	String login_url = "Login";
	String time_url = "Debug/ServerTime";
	String sync_url = "Synchronize?";
	String registered_url = "ddedfc16-39a2-45dd-ba93-f067f30c9c9b";
	String SessionID = "ddedfc16-39a2-45dd-ba93-f067f30c9c9b";
	String register_url = "Registration/";

	public WebServices(Context context) {
		this.context = context;
		c = new C(context);
	}

	public String Syncr(String lastSync, String count, String skip) {
		String syncdata = "";

		String url = host + sync_url;
		if (lastSync.length() != 0)
			url = url + "lastSyncTime=" + lastSync;
		if (count.length() != 0)
			url = url + "count=" + count;
		if (skip.length() != 0)
			url = url + "skip=" + skip;
		try {
			HttpGet request = new HttpGet(url);
			// request.setHeader("Accept", "application/json");
			// request.setHeader("Content-type", "application/json");

			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpResponse response = httpClient.execute(request);
			HttpEntity responseEntity = response.getEntity();
			InputStream stream = responseEntity.getContent();

			syncdata = c.convertStreamToString(stream);

		} catch (Exception e) {
			return e.toString();
		}

		return syncdata;
	}

	public String Registration() {
		String data = "";

		String response = "";
		String email = "\"dhiren8391@gmail.com\"";
		String phone = "0705804537";
		// String url =
		// "http://demo.foodme.ro/FoodMe/MobileClientService.svc/json/Login";

		HttpPut httpput = new HttpPut(host + register_url + phone);
		DefaultHttpClient client = new DefaultHttpClient();
		try {
			StringEntity se = new StringEntity(email, HTTP.UTF_8);
			se.setContentType("application/json");
			httpput.setHeader(HTTP.CONTENT_TYPE, "application/json");
			httpput.setEntity(se);
			HttpResponse httpResponse = client.execute(httpput);
			response = httpResponse.getStatusLine().toString();
			BufferedReader in = null;
			// data = response;
			in = new BufferedReader(new InputStreamReader(httpResponse
					.getEntity().getContent()));
			String line = "", Result = "";
			while ((line = in.readLine()) != null) {
				Result += line;
			}
			in.close();
			data = Result;

		} catch (Exception e) {
			return e.toString();
		}

		return data;
	}

	public String Login(String un, String pw, String pn) {
		String response = "";
		String data = "";
		String txt1 = "";
		// String url =
		// "http://demo.foodme.ro/FoodMe/MobileClientService.svc/json/Login";
		data = c.logindata(un, pw);

		HttpPost httppost = new HttpPost(host + login_url);
		DefaultHttpClient client = new DefaultHttpClient();
		try {
			// data = c.logindata("1234", "test");
			StringEntity se = new StringEntity(data, HTTP.UTF_8);
			se.setContentType("application/json");
			httppost.setHeader(HTTP.CONTENT_TYPE, "application/json");
			// String len = new Integer(data.length()).toString();
			httppost.setEntity(se);
			HttpResponse httpResponse = client.execute(httppost);
			response = httpResponse.getStatusLine().toString();
			BufferedReader in = null;

			in = new BufferedReader(new InputStreamReader(httpResponse
					.getEntity().getContent()));
			String line = "", Result = "";
			while ((line = in.readLine()) != null) {
				Result += line;
			}
			in.close();
			txt1 = Result;

		} catch (Exception e) {
			return e.toString();
		}
		return txt1;
	}

	public String RegisteredData() {
		String data = "";

		String url = host + SessionID;
		try {
			HttpGet request = new HttpGet(url);

			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpResponse response = httpClient.execute(request);
			HttpEntity responseEntity = response.getEntity();
			InputStream stream = responseEntity.getContent();

			data = c.convertStreamToString(stream);

		} catch (Exception e) {
			return e.toString();
		}
		return data;
	}

	public String ServerTime() {
		String time = "";

		String url = host + time_url;
		try {
			HttpGet request = new HttpGet(url);

			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpResponse response = httpClient.execute(request);
			HttpEntity responseEntity = response.getEntity();
			InputStream stream = responseEntity.getContent();

			time = c.convertStreamToString(stream);
			time=c.LongToTime(time);
			Log.v("gsdg", time);
		} catch (Exception e) {
			return e.toString();
		}
		return time;
	}
}
